/**
 * ...
 * @author Krzysztof Rozalski
 */

package hapi.util.assets;

import nme.display.BitmapData;
import nme.display.Bitmap;
import nme.media.Sound;
import nme.net.URLRequest;

class NMEAssetsLoader implements AssetsLoader {

	var assetsDirectoryPath:String;
	var cache:AssetsCache;
	
	public function new(?assetsDirectoryPath:String = "") {
		this.cache = new AssetsCache();
		this.assetsDirectoryPath = assetsDirectoryPath;
	}
	
	public function loadBitmap(src:String):Bitmap {
		return new Bitmap(loadBitmapData(src));
	}
	
	public function loadBitmapData(src:String):BitmapData {
		if (cache.bitmapDatas.exists(src)) return cache.bitmapDatas.get(src);
		var fullPath = assetsDirectoryPath + "" + src;
		var bitmapData:BitmapData = BitmapData.load(fullPath);
		cache.bitmapDatas.set(src, bitmapData);
		return bitmapData;
	}
	
	public function loadSound(src:String):Sound {
		if (cache.sounds.exists(src)) return cache.sounds.get(src);
		var fullPath = assetsDirectoryPath + "" + src;
		var sound:Sound = new Sound(new URLRequest(src));
		cache.sounds.set(src, sound);
		return sound;
	}
	
}